﻿
namespace NetTao.Analyser
{
    public class AnalyseArticle : AnalyseBase
    {
        public void AnalyseNovelInfo()
        {
            Article.articlename = MatchSingleValue(Rule.NovelName);
            Article.author = MatchSingleValue(Rule.NovelAuthor);
            string degree = MatchSingleValue(Rule.NovelDegree);
            //匹配是否完本,写作进程与NovelKeyword一致则为完本
            if(degree.Equals(Rule.NovelKeyword.Pattern))
            {
                Article.fullflag = 1;
            }
            var sortName = MatchSingleValue(Rule.LagerSort);
            Article.sortid = Config.ContentManage.Instace().GetLagerSortID(sortName);
            Article.intro = MatchSingleValue(Rule.NovelIntro).Replace("\r","").Replace("\n","");

            Article.cover = MatchSingleValue(Rule.NovelDefaultCoverUrl);
            if(string.IsNullOrEmpty(Article.cover))
            {
                Article.cover = MatchSingleValue(Rule.NovelCover);
                if(!string.IsNullOrEmpty(Article.cover) && !Article.cover.Contains(Rule.GetSiteUrl.Pattern))
                {
                    Article.cover = Rule.GetSiteUrl.Pattern + Article.cover;
                }
            }
            var novelPubKey = MatchSingleValue(Rule.NovelInfo_GetNovelPubKey);
            Article.contentslink = Rule.PubIndexUrl.Pattern.Replace("{NovelPubKey}", novelPubKey);
        }

        public string AnalyseNovelSearchUrl(string articleName)
        {
            //NovelSearchUrl 站点搜索地址 http://www.la66.com/modules/article/search.php
            //NovelSearchData 搜索提交内容  {SearchKey} 表示搜索提交的内容 searchtype=articlename&searchkey={SearchKey}
            //NovelSearch_GetNovelKey 从搜索结果中获得小说编号    {SearchKey} 表示搜索提交的内容  此获得结果存入{NovelKey}变量
            //class="bookcover"><a href="/txt(\d*)/index.html">
            var data = Rule.NovelSearchData.Pattern;
            data = data.Replace("{SearchKey}", System.Web.HttpUtility.UrlEncode(articleName,Encode));
            //data = data.Replace("{SearchKey}", System.Web.HttpUtility.UrlEncode("美",Encode));
            return string.Format("{0}?{1}", Rule.NovelSearchUrl.Pattern, data);
        }

        //查询结果有2种：a).名字完全匹配，直接跳转到article url b).查询出多个结果，正则匹配一致的 
        public string GetNovelLink(string articleName)
        {
            if (MatchSingleValue(Rule.NovelName) == "")//a)
            {
                var novelKey = MatchSingleValue(Rule.NovelSearch_GetNovelKey);
                Rule.NovelSearch_GetNovelKey.Pattern = novelKey.Replace("{SearchKey}", articleName);
                var url = MatchSingleValue(Rule.NovelSearch_GetNovelKey);
                return url;
            }
            return "";
        }
    }
}
